package btd.game;

import btd.game.activities.PBoardView;
import btd.game.activities.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.Vibrator;
import android.util.Log;

public class SoundTools {
	private SoundTools() {
	}

	// private static MediaPlayer player = null;
	private static MediaPlayer playerA = null;
	private static MediaPlayer playerB = null;
	private static MediaPlayer playerC = null;
	private static MediaPlayer playerD = null;
	private static String file = "";
	private static Context context = null;
	private static PBoardView view = null;
	public static float volume = 0.2f;

	public static boolean isCompletionA = true;
	public static boolean isCompletionB = true;
	
	private static Vibrator mVibrator = null;

	public static void play(Context context, PBoardView view, String file) {
		SoundTools.context = context;
		SoundTools.file = file;
		SoundTools.view = view;

		if ("a".equals(file) || "b".equals(file)) {
			if ("1".equals(ConstData.YINXIAO)) {
				new Thread(runnableYinxiao1).start();
			} else if ("2".equals(ConstData.YINXIAO)) {
				new Handler().post(mUpdateResults);
			}
		} else if ("c".equals(file) || "d".equals(file)) {
			if ("o".equals(ConstData.DUMIAO)) {
				new Thread(runnableDumiao).start();
			}
		}
	}

	private static Runnable runnableYinxiao1 = new Runnable() {
		public void run() {
			if (playerA == null) {
				if (file.equals("a")) {
					playerA = MediaPlayer.create(context, R.raw.a);
					playerA.setOnCompletionListener(completionListenerA);
				}
			}
			if (playerB == null) {
				if (file.equals("b")) {
					playerB = MediaPlayer.create(context, R.raw.b);
					playerB.setOnCompletionListener(completionListenerB);
				}
			}
			// if(playerC == null) {
			// if(file.equals("c")) {
			// playerC = null;
			// playerC = MediaPlayer.create(context, R.raw.b10);
			// //playerC.setOnCompletionListener(completionListenerB);
			// }
			// }
			// if(playerD == null) {
			// if(file.equals("d")) {
			// playerD = null;
			// playerD = MediaPlayer.create(context, R.raw.b212);
			// //playerD.setOnCompletionListener(completionListenerB);
			// }
			// }
			try {
				// if(playerA.isPlaying()) playerA.stop();
				// if(playerB.isPlaying()) playerB.stop();
				AudioManager am = (AudioManager) context
						.getSystemService(Context.AUDIO_SERVICE);
				int ring = am.getStreamVolume(AudioManager.STREAM_RING);
				volume = ring / 10f;
				// player.setVolume(volume, volume);
				// Log.i("Ring Volume", String.valueOf(ring));
				// if(player != null && player.isPlaying()) {
				// player.stop();
				// }
				if (file.equals("a") && !playerA.isPlaying()) {
					playerA.setVolume(volume, volume);
					playerA.start();
					Log.i("playerA", "true");
					isCompletionA = false;
					// player = playerA;
				} else if (file.equals("b") && !playerB.isPlaying()) {
					playerB.setVolume(volume, volume);
					playerB.start();
					Log.i("playerB", "true");
					isCompletionB = false;
					// player = playerB;
					// } else if(file.equals("c") && !playerC.isPlaying()) {
					// playerC.setVolume(volume, volume);
					// playerC.start();
					// Log.i("playerC", "true");
					// //isCompletionB = false;
					// //player = playerB;
					// } else if(file.equals("d") && !playerD.isPlaying()) {
					// playerD.setVolume(volume, volume);
					// playerD.start();
					// Log.i("playerD", "true");
					// isCompletionB = false;
					// player = playerB;
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
	};

	private static Runnable mUpdateResults = new Runnable() {
		public void run() {
			if (file.equals("a")) {
				mVibrator = (Vibrator) context
						.getSystemService(Context.VIBRATOR_SERVICE);
				mVibrator.vibrate(100);
			}
		}
	};

	private static Runnable runnableDumiao = new Runnable() {
		public void run() {
			// if(playerA == null) {
			// if(file.equals("a")) {
			// playerA = MediaPlayer.create(context, R.raw.a);
			// playerA.setOnCompletionListener(completionListenerA);
			// }
			// }
			// if(playerB == null) {
			// if(file.equals("b")) {
			// playerB = MediaPlayer.create(context, R.raw.b);
			// playerB.setOnCompletionListener(completionListenerB);
			// }
			// }
			// if(playerC == null) {
			if (file.equals("c")) {
				playerC = null;
				playerC = MediaPlayer.create(context, R.raw.b10);
				// playerC.setOnCompletionListener(completionListenerB);
			}
			// }
			// if(playerD == null) {
			if (file.equals("d")) {
				playerD = null;
				playerD = MediaPlayer.create(context, R.raw.b212);
				// playerD.setOnCompletionListener(completionListenerB);
			}
			// }
			try {
				// if(playerA.isPlaying()) playerA.stop();
				// if(playerB.isPlaying()) playerB.stop();
				AudioManager am = (AudioManager) context
						.getSystemService(Context.AUDIO_SERVICE);
				int ring = am.getStreamVolume(AudioManager.STREAM_RING);
				volume = ring / 10f;
				// player.setVolume(volume, volume);
				// Log.i("Ring Volume", String.valueOf(ring));
				// if(player != null && player.isPlaying()) {
				// player.stop();
				// }
				// if(file.equals("a") && !playerA.isPlaying()) {
				// playerA.setVolume(volume, volume);
				// playerA.start();
				// Log.i("playerA", "true");
				// isCompletionA = false;
				// //player = playerA;
				// } else if(file.equals("b") && !playerB.isPlaying()) {
				// playerB.setVolume(volume, volume);
				// playerB.start();
				// Log.i("playerB", "true");
				// isCompletionB = false;
				// player = playerB;
				/* } else */if (file.equals("c") && !playerC.isPlaying()) {
					playerC.setVolume(volume, volume);
					playerC.start();
					Log.i("playerC", "true");
					// isCompletionB = false;
					// player = playerB;
				} else if (file.equals("d") && !playerD.isPlaying()) {
					playerD.setVolume(volume, volume);
					playerD.start();
					Log.i("playerD", "true");
					// isCompletionB = false;
					// player = playerB;
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
	};

	private static OnCompletionListener completionListenerA = new OnCompletionListener() {
		public void onCompletion(MediaPlayer mp) {
			isCompletionA = true;
			if (view != null && (view.bDC > 0 || view.wDC > 0)) {
				if (playerB == null) {
					playerB = MediaPlayer.create(context, R.raw.b);
					playerB.setOnCompletionListener(completionListenerB);
				}
				playerB.setVolume(volume, volume);
				playerB.start();
				isCompletionB = false;
			}
		}
	};

	private static OnCompletionListener completionListenerB = new OnCompletionListener() {
		public void onCompletion(MediaPlayer mp) {
			isCompletionB = true;
		}
	};
}
